package com.xyhuo.leetcode.january;

/**
 * 构造有效字符串的最少插入次
 * https://leetcode.cn/problems/minimum-additions-to-make-valid-string/
 *
 * @time 2024/1/11
 */
public class AddMinimum {

//    public static void main(String[] args) {
//        AddMinimum am = new AddMinimum();
//        am.addMinimum("aaaacb");
//    }

    public int addMinimum(String word) {
        char[] charArray = word.toCharArray();
        int index = 0;
        int ans = 0;
        while (index < charArray.length) {
            int[] tmp = new int[3];
            int lastIndex, tmpIndex = -1, i = 0;
            for (; i < 3; i++) {
                if (index + i >= charArray.length) {
                    break;
                }
                lastIndex = tmpIndex;
                tmpIndex = charArray[index + i] - 'a';

                if (tmp[tmpIndex] == 1 || tmpIndex < lastIndex) {
                    break;
                }
                tmp[tmpIndex] = 1;
            }
            index += i;
            for (int tmpItem : tmp) {
                if (tmpItem == 0) {
                    ans++;
                }
            }
        }
        return ans;
    }
}
